﻿using System;
using System.IO;
using System.Reflection;

namespace FilterIllegalCharacter.UI
{
    public class ShortcutHelper
    {
        /// <summary>
        /// 给当前程序创建快捷方式
        /// </summary>
        /// <param name="lnkFullPath">快捷方式绝对路径</param>
        /// <param name="startupArgs">快捷方式启动参数</param>
        public static void CreateShortcut(string lnkName = "", string lnkFullPath = "", string startupArgs = "")
        {
            if (string.IsNullOrEmpty(lnkName))
            {
                lnkName = $"{Assembly.GetExecutingAssembly().GetName().Name}.lnk";
            }
            if (string.IsNullOrEmpty(lnkFullPath))
            {
                lnkFullPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), lnkName);
            }
            var shellType = Type.GetTypeFromProgID("WScript.Shell");
            dynamic shell = Activator.CreateInstance(shellType);
            var shortcut = shell.CreateShortcut(lnkFullPath);
            var exeName = $"{Assembly.GetExecutingAssembly().GetName().Name}.exe";
            var exeDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            // 工作目录和目标路径可以自由指定，注意TargetPath必须是exe的绝对路径
            shortcut.WorkingDirectory = exeDir;
            shortcut.TargetPath = Path.Combine(exeDir, exeName);
            shortcut.Arguments = startupArgs;
            shortcut.Save();
        }
    }
}
